﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModbusTestTool.Singeltons;
namespace ModbusTestTool.UI
{
    public partial class SettingsPaneV3 : UserControl
    {
        public SettingsPaneV3()
        {
            InitializeComponent();
            comboBox1.DataSource = new string[] { "Serial", "TCP/IP" };
            //SettingsSingelton.Instance.readStates();
            TagSelect.DataSource = SettingsSingelton.Instance.getListState();
            this.comboBox1.SelectionChangeCommitted += new EventHandler(comboBox1_SelectionChangeCommitted);
            this.TagSelect.SelectionChangeCommitted += new EventHandler(TagSelect_SelectionChangeCommitted);
            SetPanel(((string) TagSelect.SelectedValue));

        }

        void TagSelect_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //remove existing panel
            //find this state in our list
            //if it exists figure out what type it is
            //put the appropriate panel type

            SetPanel((string)TagSelect.SelectedValue);
        }

        private void SetPanel(string tagValue)
        {
            ModbusTestTool.Singeltons.state temp = SettingsSingelton.Instance.getState(tagValue);

            if (temp != null)
            {
                this.tagText.Text = temp.tag;
                RemoveExistingPanel();
                if (temp is stateIp)
                {
                    stateIp tempIp = temp as stateIp;

                    panel1.Controls.Add(new IpControl(tempIp.address, tempIp.port, tempIp.modbusId));
                    setPanelAnchor(panel1.Controls[0]);
                    comboBox1.SelectedIndex = 1;

                }
                else
                {
                    stateCom tempCom = temp as stateCom;
                    //SerialControl tempIpControl =  ;
                    panel1.Controls.Add(new SerialControl(tempCom.comport, tempCom.modbusId.ToString(), tempCom.baudrate.ToString()));
                    setPanelAnchor(panel1.Controls[0]);
                    comboBox1.SelectedIndex = 0;
                }
            }
        }

        void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            RemoveExistingPanel();

            if ((string)comboBox1.SelectedValue == ((string[])comboBox1.DataSource)[0])
            {
                SerialControl temp = new SerialControl("", "", "");
                //temp.ComPort
                panel1.Controls.Add(temp);
                setPanelAnchor(panel1.Controls[0]);
                Settings.Instance.State.serialOrTcpSelect = Settings.serialOrTcp.serial;
            }
            else
            {
                panel1.Controls.Add(new IpControl(string.Empty, 0, 0));
                setPanelAnchor(panel1.Controls[0]);
                Settings.Instance.State.serialOrTcpSelect = Settings.serialOrTcp.tcp;
            }
        }

        //private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        //{

        //}

        void RemoveExistingPanel()
        {
            if (panel1.Controls.Count != 0)
            {
                panel1.Controls.RemoveAt(0);
                
            }
        }

        void setPanelAnchor(Control panel)
        {
            panel.Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom);
            panel.Dock = DockStyle.Fill;
        }

        private void Add_Click(object sender, EventArgs e)
        {
            if (SettingsSingelton.Instance.getState(tagText.Text) != null)
            {
                MessageBox.Show("This Item already Exists please Enter a new name");
                return;
            }
            
            
            if ( ((string)comboBox1.SelectedValue) == "Serial")
            {
                SerialControl temp = (SerialControl)panel1.Controls[0];
                SettingsSingelton.Instance.addState(new stateCom(temp.ComPort.Text, int.Parse(temp.Baud.Text), byte.Parse(temp.ModbusId.Text), this.tagText.Text));
            }
            else
            {
                IpControl temp = (IpControl)panel1.Controls[0];
                SettingsSingelton.Instance.addState(new stateIp(temp.Address.Text, int.Parse(temp.Port.Text), byte.Parse(temp.ModbusId.Text), this.tagText.Text));
            }

            updateSelectTag();

            SettingsSingelton.Instance.saveStates();
        }

        /// <summary>
        /// Make sure the combox with tax remains consistent with view
        /// </summary>
        private void updateSelectTag()
        {
            TagSelect.DataSource = null;
            TagSelect.DataSource = SettingsSingelton.Instance.getListState();
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            SettingsSingelton.Instance.deleteState(this.tagText.Text);
            updateSelectTag();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            if (SettingsSingelton.Instance.getState(this.tagText.Text) is stateCom)
            {   SerialControl temp = (SerialControl)panel1.Controls[0];
                stateCom tempCom = SettingsSingelton.Instance.getState(this.tagText.Text) as stateCom;
                tempCom.comport = temp.ComPort.Text;
                tempCom.baudrate = int.Parse(temp.Baud.Text);
                tempCom.modbusId = byte.Parse(temp.ModbusId.Text);
            }
            else
            {
                stateIp ipTemp = SettingsSingelton.Instance.getState(this.tagText.Text) as stateIp;
                IpControl temp = (IpControl)panel1.Controls[0];
                ipTemp.tag = this.tagText.Text;
                ipTemp.port = int.Parse(temp.Port.Text);
                ipTemp.address = temp.Address.Text;
                ipTemp.modbusId = byte.Parse(temp.ModbusId.Text);
            }

            updateSelectTag();

            SettingsSingelton.Instance.saveStates();
        }


   


    }
}
